package com.arvato.wxwork.controller.front.member;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.member.WxworkMemberBindDo;
import com.arvato.wxwork.bean.bean_do.member.WxworkPhoneBookGroupDo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkExternalInfoVo;
import com.arvato.wxwork.bean.bean_vo.member.WxworkPhoneBookGroupVo;
import com.arvato.wxwork.service.member.WxworkExternalService;
import com.arvato.wxwork.service.member.WxworkMemberBindService;
import com.arvato.wxwork.service.member.WxworkPoneBookGroupService;

/**
 * 客户通讯录的Controller
 * @author ZHAN655
 */
@RestController
@RequestMapping("/wxwork/group")
public class WxworkPhoneBookGroupController{
	
	private Logger logger = LoggerFactory.getLogger(WxworkPhoneBookGroupController.class);
	
	@Autowired
	private WxworkPoneBookGroupService wxworkPoneBookGroupService; 

	@Autowired
	private WxworkExternalService wxworkExternalService;
	
	@Autowired
	private WxworkMemberBindService wxworkMemberBindService;
	
	/**
	 * 查询分组列表
	 * 查询一个staffId下面有那几个分组
	 * 分组下面有多少人
	 * @param req
	 * @param param：支持参数(keyword，brand， staffId, pageNum, pageSize)
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public List<WxworkPhoneBookGroupVo> listByGroup(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		// String staffId = query.get("staffId") + "";
		List<WxworkPhoneBookGroupVo> groupList = wxworkPoneBookGroupService.listGroupByQuery(query);
		for(WxworkPhoneBookGroupVo group: groupList) {
			// 依次根据groupId 查询其下的客户
			query.put("phoneGroupId", group.getId());
			List<WxworkExternalInfoVo> externalList = wxworkExternalService.listExternalByQuery(query);
			group.setTotal(externalList.size());
		}
		// 查询该staffId下没有被分组的客户
		List<WxworkExternalInfoVo> noGroupPeopleList = wxworkExternalService.getNoGroupPeople(query);
		WxworkPhoneBookGroupVo groupVo = new WxworkPhoneBookGroupVo();
		groupVo.setName("未分组");
		groupVo.setTotal(noGroupPeopleList.size());
		groupList.add(groupVo);
		return groupList;
	}
	
	/**
	 * 新增分组
	 * @param groupDo
	 * @param externalIdStr 会员ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert")
	public Result insert(WxworkPhoneBookGroupDo groupDo, String externalIdStr) {
		logger.info("调用新增分组接口");
		int row = wxworkPoneBookGroupService.insert(groupDo);
		if(row == 0) {
			return Result.fail("新增失败");
		}
		WxworkMemberBindDo memberBindDo = new WxworkMemberBindDo();
		for(String externalId : externalIdStr.split(",")) {
			memberBindDo.setExternalId(externalId);
			memberBindDo.setStaffId(groupDo.getStaffId());
			memberBindDo.setPhoneGroupId(groupDo.getId());
			try {
				row = wxworkMemberBindService.updateBindGroup(memberBindDo);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("新增分组添加人员到分组失败",e.getMessage());
			}
		}
		return Result.success("新增成功");
	}
	
	/**
	 * 修改分组
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update")
	public Result update(WxworkPhoneBookGroupDo dto) {
		logger.info("调用修改分组接口");
		int row = wxworkPoneBookGroupService.update(dto);
		if(row == 1) {
			return Result.success("操作成功");
		}
		return Result.fail("操作失败");
	}
	
	/**
	 * 删除分组
	 * @param id 分组id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public Result delete(String id) {
		logger.info("调用删除分组接口");
		int row = wxworkPoneBookGroupService.delete(id);
		if(row == 1) {
			return Result.success("删除成功");
		}
		return Result.fail("删除失败");
	}
	
	/**
	 * 将用户移动到其他分组
	 * @param groupId
	 * @param externalIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/transfer")
	public Result bind(String groupId, String externalIdStr) {
		WxworkMemberBindDo memberBindDo = new WxworkMemberBindDo();
		for(String externalId : externalIdStr.split(",")) {
			memberBindDo.setExternalId(externalId);
			memberBindDo.setPhoneGroupId(groupId);
			try {
				wxworkMemberBindService.updateBindGroup(memberBindDo);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("添加人员到分组失败",e.getMessage());
			}
		}
		return Result.success();
	}
}
